let punct = [0x3002, 0xFF1F, 0xFF01, 0xFF0C, 0x3001, 0xFF1B, 0xFF1A, 0x300C, 0x300D, 0x300E, 0x300F, 0x2018, 0x2019, 0x201C, 0x201D, 0xFF08, 0xFF09, 0x3014, 0x3015, 0x3010, 0x3011, 0x2014, 0x2026, 0x2013, 0xFF0E, 0x300A, 0x300B, 0x3008, 0x3009];
function isChinese(c) {
    if (c.charCodeAt(0) >= 0x4e00 && c.charCodeAt(0) <= 0x9fa5 || punct.includes(c.charCodeAt(0))) {
        return true;
    } else {
        return false;
    }
}
let dict = require('./zh-CN.json');
const fs = require('fs');
function findJSON(value) {
    // JSON 对象的反向查询，返回 key
    let arr = Object.values(dict);
    let index = arr.indexOf(value);
    if (index === -1) {
        let rand = Math.random().toString(36).slice(2);
        dict[rand] = value;
        return rand;
    }
    let keys = Object.keys(dict);
    fs.writeFile('./newLocale.json', JSON.stringify(dict), function (err) {
        if (err) {
            console.log(err)
        }
    })
    return keys[index];
}

module.exports = function ({ types: babelTypes }) {
    return {
        name: "deadly-simple-plugin-example",
        inherits: () => {
            return {
                manipulateOptions(opts, parserOpts) {
                    // parserOpts.plugins.push("babel-plugin-syntax-jsx");
                    // parserOpts.plugins.push("plugin-proposal-class-properties");
                }
            };

        },
        visitor: {
            JSXText(path, state) {
                let node = path.node;
                let flag = false;

                for (let i = 0; i < path.node.value.length; i++) {
                    if (isChinese(node.value[i])) {
                        flag = true;
                        break;
                    }
                }
                if (flag) {
                    let val = /\S.*\S/.exec(path.node.value);// 过滤两头的空白字符
                    if (val) {
                        val = val[0]
                        path.node.value = path.node.value.replace(val, '{intl.get(\'' + findJSON(val) + '\')}');
                    }
                }
            },
            // JSXAttribute(path, state) {
            //     console.log('----------JSXAttribute-----------');
            //     console.log(path.node);

            // },
            // JSXExpressionContainer(path, state) {
            //     console.log('----------JSXExpressionContainer-----------');
            //     console.log(path.node.expression.arguments);

            // },
            Literal(path, state) {
                let node = path.node

                if (node.type === 'StringLiteral') {
                    let flag = false;

                    for (let i = 0; i < path.node.value.length; i++) {
                        if (isChinese(node.value[i])) {
                            flag = true;
                            break;
                        }
                    }
                    if (flag) {
                        let val = /\S.*\S/.exec(path.node.value);// 过滤两头的空白字符
                        if (val) {
                            val = val[0]
                            path.node.value = path.node.value.replace(val, 'intl.get(\'' + findJSON(val) + '\')');
                        }
                    }
                }
            }
        }
    };
};